package ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ToolTipManager;
import javax.swing.text.AbstractDocument;

/** Representing the ui for adding an account
 * @author Matthias
 * @author Niels Christian Mouritsen
 */
public class UserMenuAdd extends JPanel {
	
	private final JLabel lbl_Header;
	private final JTextField textField_AccNr;
	private final JLabel lbl_Intro;
	private final JLabel lbl_Choice;
	private final JRadioButton rdbtn_Credit;
	private final JRadioButton rdbtn_Savings;
	private final JRadioButton rdbtn_Depot;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final JButton btn_Back;
	private final JButton btn_Submit;
	private final JLabel logo_ATM;
	
	/**
	 * Contains the information/blueprint on labels, buttons, radio buttons and text fields
	 * Sets the window Dimensions and layout to null
	 */
	public UserMenuAdd() {
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);
		
		lbl_Header = new JLabel("Add an account");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(215, 25, 194, 34);
		add(lbl_Header);
		
		textField_AccNr = new JTextField();
		textField_AccNr.addMouseListener(mouseListener);
		textField_AccNr.setToolTipText("6 digits required");
		((AbstractDocument)textField_AccNr.getDocument()).setDocumentFilter(new LimitOfChar(6));
		textField_AccNr.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_AccNr.setBounds(93, 122, 160, 30);
		add(textField_AccNr);
		textField_AccNr.setColumns(10);
		
		lbl_Intro = new JLabel("Please type in your preferred accountnumber");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(93, 91, 344, 21);
		add(lbl_Intro);
		
		lbl_Choice = new JLabel("Please select an account type");
		lbl_Choice.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Choice.setBounds(93, 172, 242, 21);
		add(lbl_Choice);
		
		rdbtn_Credit = new JRadioButton("Credit");
		rdbtn_Credit.addMouseListener(mouseListener);
		rdbtn_Credit.setToolTipText("Only one Credit account allowed");
		rdbtn_Credit.setFont(new Font("SansSerif", Font.BOLD, 16));
		buttonGroup.add(rdbtn_Credit);
		rdbtn_Credit.setBounds(176, 200, 80, 23);
		add(rdbtn_Credit);
		
		rdbtn_Savings = new JRadioButton("Savings");
		rdbtn_Savings.addMouseListener(mouseListener);
		rdbtn_Savings.setToolTipText("Only one Savings account allowed");
		rdbtn_Savings.setFont(new Font("SansSerif", Font.BOLD, 16));
		buttonGroup.add(rdbtn_Savings);
		rdbtn_Savings.setBounds(275, 200, 87, 23);
		add(rdbtn_Savings);
		
		rdbtn_Depot = new JRadioButton("Depot");
		rdbtn_Depot.addMouseListener(mouseListener);
		rdbtn_Depot.setToolTipText("Only one Depot account allowed");
		rdbtn_Depot.setFont(new Font("SansSerif", Font.BOLD, 16));
		buttonGroup.add(rdbtn_Depot);
		rdbtn_Depot.setBounds(380, 200, 80, 23);
		add(rdbtn_Depot);
		
		btn_Submit = new JButton("Submit");
		btn_Submit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Submit.setBounds(380, 350, 130, 50);
		add(btn_Submit);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(93, 350, 130, 50);
		add(btn_Back);
		
		logo_ATM = new JLabel("");
		logo_ATM.setIcon(new ImageIcon(UserMenuAdd.class.getResource("/pic/LOGO Mini.png")));
		logo_ATM.setBounds(15, 15, 50, 50);
		add(logo_ATM);
	}
	
	/** Adds action listeners, in order to define an action 
	 * when a component is called
	 * @param e
	 */
	public final void addActionListener(final ActionListener e) {
		btn_Back.addActionListener(e);
		btn_Submit.addActionListener(e);
	}
	
	/**
	 * Clears the text field
	 */
	public final void clearFields() {
		textField_AccNr.setText("");
		buttonGroup.clearSelection();
	}

	/**
	 * @return input in the text field, used as an account number
	 */
	public final int getAccNr() {
		int accNr = 0;
		try {
			accNr = Integer.parseInt(textField_AccNr.getText().trim());
		} catch (Exception exc) {
		}
		return accNr;
	}
	
	/**
	 * @return back button
	 */
	public final JButton getBtnBack() {
		return btn_Back;
	}
	
	/**
	 * @return submit button
	 */
	public final JButton getBtnSubmit() {
		return btn_Submit;
	}

	/**
	 * @return radio button credit
	 */
	public final JRadioButton getRdbtnCredit() {
		return rdbtn_Credit;
	}

	/**
	 * @return radio button savings 
	 */
	public final JRadioButton getRdbtnSavings() {
		return rdbtn_Savings;
	}

	/**
	 * @return radio button for depot
	 */
	public final JRadioButton getRdbtnDepot() {
		return rdbtn_Depot;
	}
	
	/** Adding a mouselistener to the class, 
	 * by instantiating a MouseAdapter in an anonymous class 
	 * When the mouse is hovering over an object, the tooltip is enabled
	 * When the mouse is not hovering over an object, the tooltop is disabled
	 */
	private final MouseListener mouseListener = new MouseAdapter() {    
	    private final int defaultTimeout = 
	    		ToolTipManager.sharedInstance().getInitialDelay();

	    @Override
	    public void mouseEntered(final MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(0);
	    }

	    @Override
	    public void mouseExited(final MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
	    }
	};
	
} // end this class